package org.crmsystem.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.dao.ProKindMapper;
import org.crmsystem.model.Contact;
import org.crmsystem.model.OrgInfo;
import org.crmsystem.model.Pact;
import org.crmsystem.model.Page;
import org.crmsystem.model.ProKind;
import org.crmsystem.model.Staff;
import org.crmsystem.model.User;
import org.crmsystem.service.IContactService;
import org.crmsystem.service.IOrgService;
import org.crmsystem.service.IPactService;
import org.crmsystem.service.IProKindService;
import org.crmsystem.service.IStaffService;
import org.crmsystem.service.IUserService;
import org.crmsystem.util.DateUtil;
import org.crmsystem.util.GeneraterKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 合同（pact）控制层Controller
 * 
 * @author 袁立新
 *
 */

@Controller
@RequestMapping("/pact")
public class PactController {
	private static Logger logger = LogManager.getLogger(ProKindMapper.class);
	/**
	 * 合同表接口 属性
	 * 
	 */
	@Autowired
	private IPactService iPactService;
	/**
	 * 产品类别表接口 属性
	 */
	@Autowired
	private IProKindService iProKindService;
	/**
	 * 机构信息表接口 属性
	 */
	@Autowired
	private IOrgService iOrgService;
	/**
	 * 员工信息表接口  属性
	 */
	@Autowired
	private IUserService iUserService;
    /**
     * 联系人表接口  属性
     */
    private IContactService iContactService;
    
    /**
     * 员工表接口  属性
     */
    private IStaffService iStaffService;
	/**
	 * 时间对象  属性
	 */
	DateUtil dateUtil = new DateUtil();

	/**
	 * 添加合同页面
	 * 
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/add")
	public ModelAndView getIndex(String id) throws ParseException {
		List<OrgInfo> orgList=iOrgService.selectOrgList(null);//机构
		List<ProKind> proKinds = iProKindService.select();//产品类别	
		Map<String,Object> map=new HashMap<String, Object>();
		List<Contact> contacts=null;
		try{
			//需要捕获异常的代码
			 contacts = (List<Contact>) iContactService.getContactById(Integer.parseInt(id));//联系人
			} catch (Exception e) {
			System.out.println(e.getMessage());
			map.put("contacts", contacts);
			}				
		map.put("orgList", orgList);
		map.put("proKinds", proKinds);	
		return new ModelAndView("addPact",map);
		
	}
	

	/**
	 * 合同列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/showPact")
	public ModelAndView getIndex1(String id) {
		ArrayList<Pact> pacts = (ArrayList<Pact>) iPactService.findPacts(null);	
		ArrayList<OrgInfo> orgList=iOrgService.selectOrgList(null);//机构
		ArrayList<ProKind> proKinds = (ArrayList<ProKind>) iProKindService.select();//产品类别
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("orgList", orgList);
		map.put("proKinds", proKinds);
		
		ModelAndView moView = new ModelAndView("pactList");
		moView.addObject("pacts", pacts);
		moView.addObject("opList", map);
		return moView;
	}

	/**
	 * 添加合同action
	 * 
	 * @param pact
	 * @param
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/savePact",method=RequestMethod.POST)
	public String insertPact(String id,String name,int OrgInfoId,int proKinds,String property,
	String kind,int operatorId,int contactId,double totalAmount,String payWay,String date, String beginDate,
	String endDate,String signB,String signA,String remark,String checkState) throws Exception{
		Pact pact=new Pact();
		//String id=GeneraterKey.str2md5("p");
		pact.setId(id);
		pact.setCheckState(checkState);
		pact.setContactId(contactId);
		pact.setId(id);
		pact.setKind(kind);
		pact.setName(name);
		pact.setOperatorId(operatorId);
		pact.setOrgId(OrgInfoId);
		pact.setPayWay(payWay);
		pact.setProKindId(proKinds);
		pact.setProperty(property);
		pact.setRemark(remark);
		pact.setSignA(signA);
		pact.setSignB(signB);
		pact.setTotalAmount(totalAmount);
		Date date1;
		if(!"".equals(date)){
			date1=DateUtil.formatDate(date, "yyyy-MM-dd");
		}else{
			date1=new Date();
		}
		pact.setDate(date1);
		Date beginDate1;
		if(!"".equals(beginDate)){
			beginDate1=DateUtil.formatDate(beginDate, "yyyy-MM-dd");
		}else{
			beginDate1=new Date();
			
		}
		pact.setBeginDate(beginDate1);
		Date endDate1;
		if(!"".equals(endDate)){
			endDate1=DateUtil.formatDate(endDate, "yyyy-MM-dd");
		}else{
			endDate1=new Date();
		}
		pact.setEndDate(endDate1);
		int flag=iPactService.savePact(pact);
		return "redirect:/pact/showPact";
	}
		
    //public String savePact() throws Exception{iPactService.savePact(null);return "forward:/pact/showPact"}
	/**
	 * 删除合同
	 * 
	 * @param pactId
	 * @return
	 */
	@RequestMapping("/delePact")
	@ResponseBody
	public String delPact(String pactId) {
		int flag=iPactService.delete(pactId);
		String msg = null;
        logger.info(flag + "======");
        if (flag == 1) {
            msg = "SUCC";
        } else {
            msg = "";
        }
        return msg;   /*"forword:/pact/showPact"*/
	}

	/**
	 * 点击跳转到   修改编辑合同
	 * 
	 * @param pactId
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/updatePact")
	public ModelAndView updatePact(String  pactId) throws ParseException {
		Pact pact=iPactService.findPactById(pactId);
		ModelAndView mView=new ModelAndView("addPact");
		Map<String,Object> map=new HashMap<String, Object>();
		List<OrgInfo> orgLists=iOrgService.selectOrgList(null);//机构
		List<ProKind> proKinds = iProKindService.select();//产品类别
		map.put("orgList", orgLists);
		map.put("proKinds", proKinds);				    	    	
		mView.addObject("opList",map);
		mView.addObject("pact",pact);								
		return mView;
			
	}

	/**
	 * 多条件查询
	 * @return
	 */
	@RequestMapping("/findPacts")
	@ResponseBody
	public HashMap<String,Object> fingPacts(){
		
		return null;
	}
	
	/*@RequestMapping("/getOListBySearch")
	@ResponseBody
	public HashMap<String, Object> getOListBySearch(String search,String searchText,int currPage,int pageSize) {
		HashMap<String, Object> searchMap=new HashMap<String, Object>();
		searchMap.put("search", search);
		searchMap.put("searchText", searchText);
		searchMap.put("pageSize", pageSize);
		searchMap.put("currPage", currPage);
		ArrayList<OrgInfo> oList=iOrgService.selectOrgList(searchMap);
		HashMap<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("total", iOrgService.getOrgCount(searchMap));
		resultMap.put("list", oList);
		return resultMap;
	}
	*/
	
	
	
	/**
	 * 封装四个list方法
	 *
	 */
	public ModelAndView fourList(String pathStr) {
		ArrayList<Contact> contacts=iContactService.selectContactList(null);//联系人列表
		Map map=new HashMap();
    	map.put("user", new User());//用户信息集合
    	map.put("pageSize", 10);
    	map.put("currPage", 1);
		List<User> users=(List<User>) iUserService.getUser(map);//员工列表
		List<Pact> pacts= iPactService.findPacts(null);//合同列表	
		List<ProKind> proKinds=iProKindService.select();//产品列表
		System.out.println(proKinds);
		ModelAndView mView=new ModelAndView(pathStr);//模型和视图
		mView.addObject("contactList",contacts);//联系人
		mView.addObject("userList",users);//用户
		mView.addObject("pactList",pacts);//合同		
		return mView;
	}

}
